//
//  NEDFormSubItem.m
//  NEDFormView_Example
//
//  Created by 000 on 2022/1/7.
//  Copyright © 2022 000. All rights reserved.
//

#import "NEDFormContentItem.h"
#import "NEDFormContentItem+Privite.h"
#import "NEDFormItem.h"
#import <NEDConstant/NEDConstant.h>
#import <NEDStyle/UIColor+NEDStyle.h>
#import <NEDStyle/UIFont+NED.h>

UIKIT_EXTERN NSString * NEDFormContentShowTypeDes(NEDFormContentShowType showType){
    if(showType == NEDFormContentShowType_Switch)   return @"开关";
    if(showType == NEDFormContentShowType_Title)   return @"文本";
    if(showType == NEDFormContentShowType_Image)   return @"图片";
    return @"未知";
}

@interface NEDFormContentItem()

@property (assign, nonatomic, readwrite) NEDFormContentShowType showType;
/// 所属位置
@property (assign, nonatomic, readwrite) NEDFormContentShowPosition position;
/// 布局信息
@property (retain, nonatomic, readwrite) NEDFormContentLayout * layout;

@end

@implementation NEDFormContentItem

- (instancetype)initWithType:(NEDFormContentShowType)showType position:(NEDFormContentShowPosition)position belongFormSectionItem:(NEDFormItem *)sectionItemMain{
    self = [super init];
    if(self){
        self.showType = showType;
        self.position = position;
        
        self.layout = [NEDFormContentLayout new];
        
        if(position == NEDFormContentShowPosition_Left){
            self.layout.titleAlignment = NSTextAlignmentLeft;
            self.layout.titleFont = [UIFont ned_pfOfSize:15];
            self.layout.titleColor = UIColor.ned_h1;
        }
        else if(position == NEDFormContentShowPosition_Center){
            self.layout.titleAlignment = NSTextAlignmentCenter;
            self.layout.titleFont = [UIFont ned_pfOfSize:15];
            self.layout.titleColor = UIColor.ned_h1;
        }
        else if(position == NEDFormContentShowPosition_Right){
            self.layout.titleAlignment = NSTextAlignmentRight;
            self.layout.titleFont = [UIFont ned_pfOfSize:14];
            self.layout.titleColor = UIColor.ned_h3;
        }
        
        if(sectionItemMain){
            [self setBelongFormItem:^NEDFormItem * _Nonnull{
                return sectionItemMain;
            }];
        }
        
        if(showType == NEDFormContentShowType_Title){
            [self setInitViewBlock:^UIView * _Nonnull(NEDFormItem * _Nonnull sectionItem, NEDFormContentItem * _Nonnull contentItem) {
                UILabel * label = [UILabel new];
                label.textAlignment = contentItem.layout.titleAlignment;
                label.font = contentItem.layout.titleFont;
                label.textColor = contentItem.layout.titleColor;
                if(contentItem.styleBlock){
                    contentItem.styleBlock(label,sectionItem,contentItem);
                }
                return label;
            }];
        }
        else if(showType == NEDFormContentShowType_Image){
            [self setInitViewBlock:^UIView * _Nonnull(NEDFormItem * _Nonnull sectionItem, NEDFormContentItem * _Nonnull contentItem) {
                UIImageView * imageView = [UIImageView new];
                imageView.contentMode = UIViewContentModeScaleAspectFit;
                if(contentItem.styleBlock){
                    contentItem.styleBlock(imageView,sectionItem,contentItem);
                }
                return imageView;
            }];
        }
        else if(showType == NEDFormContentShowType_Switch){
            [self setInitViewBlock:^UIView * _Nonnull(NEDFormItem * _Nonnull sectionItem, NEDFormContentItem * _Nonnull contentItem) {
                UISwitch * switchView = [UISwitch new];
                if(contentItem.styleBlock){
                    contentItem.styleBlock(switchView,sectionItem,contentItem);
                }
                return switchView;
            }];
        }
        else if(showType == NEDFormContentShowType_Custom){
            //自定义 可外部重写initViewBlock
            [self setInitViewBlock:^UIView * _Nonnull(NEDFormItem * _Nonnull sectionItem, NEDFormContentItem * _Nonnull contentItem) {
                UIView * view = [UIView new];
                if(contentItem.styleBlock){
                    contentItem.styleBlock(view,sectionItem,contentItem);
                }
                return view;
            }];
        }
    }
    return self;
}


-(void)setContentTitle:(NSString *)contentTitle{
    _contentTitle = contentTitle;
    if(self.changeBlock){
        self.changeBlock(self.__belongFormSectionItem, self);
    }
}
-(void)setContentImageURL:(NSString *)contentImageURL{
    _contentImageURL = contentImageURL;
    if(self.changeBlock){
        self.changeBlock(self.__belongFormSectionItem, self);
    }
}
-(void)setContentSwitchIsOn:(BOOL)contentSwitchIsOn{
    _contentSwitchIsOn = contentSwitchIsOn;
    if(self.changeBlock){
        self.changeBlock(self.__belongFormSectionItem, self);
    }
}
-(void)setContentLinkDataInfo:(id)contentLinkDataInfo{
    _contentLinkDataInfo = contentLinkDataInfo;
    if(self.changeBlock){
        self.changeBlock(self.__belongFormSectionItem, self);
    }
}

- (BOOL)isAvailable{
    if(self.availableBlock){
        return self.availableBlock(self.__belongFormSectionItem,self);
    }
    return YES;
}

#pragma mark - privite

- (NEDFormItem *)__belongFormSectionItem{
    if(self.belongFormItem){
        return self.belongFormItem();
    }
    return nil;
}

@end
